<?php


namespace app\appa\controller;


use app\appa\model\PetModel;
use app\appa\validate\PetValidate;
use app\common\controller\Api;
use think\Request;

class Pet extends Api
{
    public function index(Request $request)
    {
//        $master_id = input('master_id/d');
//        if (empty($master_id)){
//            $this->error('没有主人的id');
//        }
        $order = PetModel::where('master_id',$request->userId)->field('id,name,type,image')->select();
        if (!$order){
            $this->error('没有宠物信息');
        }
        $this->success('查询成功',$order);
    }

    public function list(Request $request)
    {
        $id = input('id/d');
        if (empty($id)){
            $this->error('宠物id未获取');
        }
        $order = PetModel::field('id,name,type,age,sex,shape,description,remark,image')
            ->find($id);
        if (!$id){
            $this->error('未查询到信息');
        }
        $this->success('查询成功',$order);
    }

    public function add(Request $request)
    {
        $data = input('post.');
        //验证数据
        $validate = new PetValidate();
        $result = $validate->scene('add')->check($data);
        if ($result){
            $this->error($validate->getError());
        }
        $add = PetModel::where('name',$data['name'])
            ->where('master_id',$request->userId)
            ->find();
        if ($add){
            $this->error('宠物已经存在');
        }
        $data['master_id'] = $request->userId;
        $data['create_time'] = date('Y-m-d H:i:s');
        if (PetModel::create($data)){
            $this->success('添加成功');
        }
        $this->error('添加失败 ');
    }

    public function edit(Request $request)
    {
        $id = input('id/d');
        if (empty($id)){
            $this->error('id不能为空');
        }
        $add = PetModel::field('id,name,type,age,sex,description,remark,shape,image')->find($id);
        if (!$add){
            $this->error('查询失败');
        }
        $this->success('查询成功',$add);
    }

    public function update(Request $request)
    {
        $data = input('post.');
        //验证数据
        $validate = new PetValidate();
        $result = $validate->scene('edit')->check($data);
//        halt($result);
        if (!$result){
            $this->error($validate->getError());
        }
        $user = PetModel::find($data['id']);
        if (!$user){
            $this->error('该宠物信息不存在');
        }
        $user->name = $data['name'];
        $user->type = $data['type'];
        $user->age = $data['age'];
        $user->sex = $data['sex'];
        $user->description = $data['description'];
        $user->remark = $data['remark'];
        $user->shape = $data['shape'];
        $user->image = $data['image'];
        $user->update_time = date('Y-m-d H:i:s');
        if ($user->save()){
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }

    public function delete()
    {
        $id = input('id/d');
        if (empty($id)){
            $this->error('id不能为空');
        }
        $news =  PetModel::find($id);
        if (!$news) {
            $this->error('宠物信息不存在');
        }
        if ($news->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}